<?php


namespace app\queue\stomp;


use app\queue\BaseConsumer;
use common\model\EntityModel;
use common\service\EntityRelationshipService;
use Webman\Stomp\RetryAbleConsumer;

/**
 * 同步关联关系到实体关系表
 * Class SyncDesignRelationData
 * @package app\queue\redis
 */
class SyncRelationDataToEntityRelationShips extends BaseConsumer implements RetryAbleConsumer
{

    // 要消费的队列名
    public $queue = 'sync_relation_data_to_entity_relation_ships';

    // 连接名，对应 config/redis_queue.php 里的连接`
    public $connection = 'default';

    /**
     * @param $data
     * @return void
     * @throws \Throwable
     */
    public function doConsume($data)
    {
        $entity = model(EntityModel::class)->find($data['id']);
        if (empty($entity)) {
            return;
        }
        // 解析当前实体 获得关联字段
        $entityModule = module_by_id($entity['module_id']);
        $entityJson = json_decode($entity['json'], true);
        $relationFields = [];
        $textFields = false;
        switch ($entityModule['code']) {
            case 'design':
                $category = $entityJson['design_category'] ?? null;
                if (empty($category)) {
                    return;
                }
                switch ($category) {
                    case '角色':
                    case '道具':
                    case '场景':
                        break;
                    case '关卡':
                        $relationFields = [
                            'level_relate_asset',
                        ];
                        break;
                }
                $textFields = true;
                break;
            case 'asset':
            case 'level':
            case 'storyboard':
            case 'shot':
                $relationFields = [
                    $entityModule['code'] . '_asset',
                    $entityModule['code'] . '_level',
                ];
                break;
        }
        if (empty($relationFields)) {
            return;
        }
        // 根据关联字段 解析出来关联的数据
        $relationList = [];
        foreach ($relationFields as $field) {
            if (empty($entityJson[$field])) {
                continue;
            }
            $tempRelationList = array_filter(explode(',', $entityJson[$field]));
            if (empty($tempRelationList)) {
                continue;
            }
            $relationList = array_merge($relationList, $tempRelationList);
        }
        // 如果是文本关联数据 需要转换成id
        if ($textFields) {
            $relationList = model(EntityModel::class)
                ->where([
                    'project_id' => $entity['project_id'],
                    'module_id' => module_by_code('design')['id'],
                    'name' => ['IN', implode(',', $relationList)],
                ])
                ->field('id')
                ->column('id');
        }
        // 保存关联数据
        EntityRelationshipService::getInstance()->saveEntityRelationshipList($entity['id'], $relationList);
    }

    /**
     * @return int
     */
    public function getTtr()
    {
        return 10;
    }

    /**
     * @param $attempt
     * @param $error
     * @return bool
     */
    public function canRetry($attempt, $error)
    {
        trace(format_exception_info($error), 'ERR');
        return $attempt <= 3;
    }
}
